/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2018-12-13     balanceTWK    add sdcard port file
 * 2019-08-23     WillianChan   add spi sdcard port code
 */

#include <rtthread.h>

#if defined BSP_USING_SDIO_SDCARD || defined BSP_USING_SPI_SDCARD
#include <dfs_elm.h>
#include <dfs_fs.h>
#include <dfs_posix.h>
#include "drv_gpio.h"

#define DBG_TAG "app.card"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

void sd_mount(void *parameter)
{
	uint8_t i,j = 0;
    while (i<5)
    {
        rt_thread_mdelay(2000);
	
		if(dfs_mount("W25Q16JV", "/", "elm", 0, 0) == RT_EOK)
		{
			LOG_I("W25Q16JV mount to '/' dir.");
			if(mkdir("/sd", 0x777) != 0)
				LOG_E("creat '/sd' dir fail !");
			else
				LOG_D("creat '/sd' dir.");
			while(j < 5)
			{
				if(rt_device_find("sd0") != RT_NULL)
				{
					if (dfs_mount("sd0", "/sd", "elm", 0, 0) == RT_EOK)
					{
						LOG_D("sd card mount to '/sd' dir.");
						break;
					}
					else
					{
						LOG_W("sd card mount to '/sd' fail!retry...");
						j++;
					}
				}
			}
			break;
		}
		else
		{
			LOG_W("W25Q16JV mount to '/' fail!retry...");
			i++;
		}
		
    }
	return;
}

int stm32_sdcard_mount(void)
{
    rt_thread_t tid;

    tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
                           1024, RT_THREAD_PRIORITY_MAX - 2, 20);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        LOG_E("create sd_mount thread err!");
    }
    return RT_EOK;
}
//INIT_ENV_EXPORT(stm32_sdcard_mount);

#endif /* BSP_USING_SDIO_SDCARD || BSP_USING_SPI_SDCARD */

#ifdef BSP_USING_SPI_SDCARD
#include "drv_spi.h"
#include "spi_msd.h"
static int rt_hw_spi1_tfcard(void)
{
    __HAL_RCC_GPIOD_CLK_ENABLE();
    rt_hw_spi_device_attach("spi1", "spi11", GPIOB, GPIO_PIN_4);
    return msd_init("sd0", "spi11");
}
INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);
#endif /* BSP_USING_SPI_SDCARD */
